// pages/inspectItemPatrol/inspectItemPatrol.js
const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: 1
  },
  onLoad: function(options) {
    wx.showLoading({
      title: '数据加载中',
    })
    this.setData({
      macAddress:options.bluetooth
    })
  },

  onShow: function(options) {
    // submitRefresh?
    wx.request({
      url: app.inspectUrl + '/WeChat/weChatSystem/getInspectInfo',
      method: 'POST',
      data: {
        schoolId: app.globalData.departmentId,
        userId: app.globalData.userId,
        identityId: app.globalData.identityId,
        macAddress: this.data.macAddress
        // macAddress: "35:10064"
      },
      dataType: 'json',
      success: res => {
        console.log(res)
        let result = [],
          done = [],
          undone = [],
          resDataLength = res.data.length
        for (let i = 0; i < resDataLength; i++) {
          let resDataItemListLength = res.data[i].itemList.length
          for (let j = 0; j < resDataItemListLength; j++) {
            if (res.data[i].itemList[j].undone == 0) {
              done.push(res.data[i].itemList[j])
            } else {
              undone.push(res.data[i].itemList[j])
            }
          }
        }
        undone.sort(function(a, b) {
          if (b.deadline.substring(5, 7) == a.deadline.substring(5, 7)) {
            if (b.deadline.substring(8, 10) == a.deadline.substring(8, 10)) {
              return a.address > b.address ? 1 : -1
            } else {
              return b.deadline.substring(8, 10) - a.deadline.substring(8, 10)
            }
          } else {
            return b.deadline.substring(5, 7) - a.deadline.substring(5, 7)
          }
        })
        done.sort(function(a, b) {
          return b.finLong - a.finLong
        })
        result = undone.concat(done)

        this.setData({
          list: result
        })
        wx.setStorageSync('inspectPatrol', result)
        wx.hideLoading()
        this.setData({
          loading: 0
        })
      },
      fail: res => {
        wx.showToast({
          title: '网络错误！',
          image: '/images/warning.png'
        })
      }
    })
  },

  showEvent: function(e) {
    // console.log(e)
    let itemIndex = parseInt(e.currentTarget.id),
      showChecked = "list[" + itemIndex + "].showChecked",
      select = "list[" + itemIndex + "].select",
      item = this.data.list[itemIndex],
      uncheckedNum = 0,
      checkedNum = 0,
      selectCheck = null
    for (let i = 0; i < item.idAndName.length; i++) {
      if (item.idAndName[i].checked == 0) {
        uncheckedNum++;
      } else {
        checkedNum++;
      }
    }
    if (e.currentTarget.dataset.checked == 0) {
      selectCheck = checkedNum
    } else if (e.currentTarget.dataset.checked == 1) {
      selectCheck = uncheckedNum
    }
    this.setData({
      [select]: selectCheck,
      [showChecked]: e.currentTarget.dataset.checked
    })
  },

  bindWorkDetail: util.throttle(function(e) {
    // console.log(e.currentTarget)
    if (e.currentTarget.dataset.checked == 0) {
      wx.navigateTo({
        url: `/pages/workInspectDetail/workInspectDetail?inspectPlanId=${e.currentTarget.dataset.inspectplanid}&categoryId=${e.currentTarget.dataset.categoryid}&checked=${e.currentTarget.dataset.checked}&address=${e.currentTarget.dataset.address}&className=${e.currentTarget.dataset.classname}`
      })
    } else {
      wx.navigateTo({
        url: '/pages/workInspectHistory/workInspectHistory?inspectPlanId=' + e.currentTarget.dataset.inspectplanid + '&categoryId=' + e.currentTarget.dataset.categoryid + '&checked=' + e.currentTarget.dataset.checked
      })
    }
  })
})